package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.exception.GuidanceException;
import com.woniuxy.commons.exception.LoginPhoneException;
import com.woniuxy.commons.exception.RegisterException;
import com.woniuxy.commons.utile.BeanCopyUtil;
import com.woniuxy.commons.utile.Result;
import com.woniuxy.controller.dto.AccountDto;
import com.woniuxy.controller.dto.GetGuidanceDto;
import com.woniuxy.controller.form.AccountForm;
import com.woniuxy.controller.form.GetGuidanceForm;
import com.woniuxy.controller.form.RepairForm;
import com.woniuxy.controller.form.RepairGuidanceForm;
import com.woniuxy.dao.model.Account;
import com.woniuxy.dao.model.Guidance;
import com.woniuxy.dao.model.RepairGuidance2;
import com.woniuxy.service.GuidanceService;
import com.woniuxy.service.RepairGuidance2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 服务指南 前端控制器
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/repairGuidance2")
@Api(tags = "服务指南接口")
public class RepairGuidance2Controller {

    @Resource
    private RepairGuidance2Service repairGuidance2Service;

    @Resource
    private GuidanceService guidanceService;

    @PostMapping("/addGuidance")
    @ApiOperation("新增服务指南")
    private Result register(@Valid @RequestBody RepairGuidanceForm repairGuidanceForm) throws MessagingException {
        RepairGuidance2 repairGuidance2 = BeanCopyUtil.copyOne(repairGuidanceForm, RepairGuidance2.class);
        AccountDto accountDto = (AccountDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        repairGuidance2.setPublisher(accountDto.getAccountName());
        boolean save = repairGuidance2Service.save(repairGuidance2);
        if (!save) throw new GuidanceException(1005,"新增服务指南失败");
        return Result.instance().setCode(200).setMessage("新增成功");
    }

    @PostMapping("/getGuidance")
    @ApiOperation("获取服务指南（通过大标题ID,页数，条数）")
    private Result getGuidance(@Valid @RequestBody GetGuidanceForm getGuidanceForm) {
        System.out.println(getGuidanceForm);
        //参数校验
        if (getGuidanceForm.getGuidanceId()==null || getGuidanceForm.equals(""))getGuidanceForm.setGuidanceId(1);
        if (getGuidanceForm.getPageNum()==null || getGuidanceForm.getPageNum().equals(""))getGuidanceForm.setPageNum(1);
        if (getGuidanceForm.getPageSize()==null || getGuidanceForm.getPageSize().equals(""))getGuidanceForm.setPageSize(5);
        Page<GetGuidanceDto> page = repairGuidance2Service.getGuidance(getGuidanceForm);
        return Result.instance().setCode(200).setMessage("获取成功").setData(page);
    }

    /**
     * 获取服务指南大标题
     * @param
     * @return
     */
    @GetMapping("/getLargesHeadline")
    @ResponseBody
    @ApiOperation("获取服务指南大标题")
    public Result getLargesHeadline(){
        List<Guidance> list = guidanceService.list();
        return Result.instance().setCode(200).setMessage("成功").setData(list);
    }
}

